package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.JKNiaosuan;
import com.api.shopapi.entity.MyResult;
import com.api.shopapi.entity.PreCheck;
import com.api.shopapi.entity.Staff;
import com.api.shopapi.entity.dto.JKNiaosuanDTO;
import com.api.shopapi.service.JKDanguchunService;
import com.api.shopapi.service.JKNiaosuanService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.util.DateUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RequestMapping("JKNiaosuan")
@RestController
public class JKNiaosuanController extends BaseController{


    @Autowired
    @Qualifier("JKNiaosuanServiceImplOne")
    private JKNiaosuanService jkNiaosuanService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;


    //添加用户尿酸检查记录
    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKNiaosuan/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            String niaoSuan = jsonObject.getString("niaoSuan");
            int fromType = jsonObject.getInteger("fromType");
            Date inspectTime = jsonObject.getDate("inspectTime");
            JKNiaosuan jkNiaosuan=new JKNiaosuan();
            jkNiaosuan.setJKNiaosuanCode(UUID.randomUUID().toString());
            jkNiaosuan.setUserCode(userCode);
            jkNiaosuan.setNiaoSuan(niaoSuan);
            jkNiaosuan.setFromType(fromType);
            jkNiaosuan.setInspectTime(inspectTime);
            jkNiaosuan.setCreateTime(new Date());
            jkNiaosuanService.insert(jkNiaosuan);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKNiaosuan/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //修改用户尿酸检查记录
    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKNiaosuan/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String jkNiaosuanCode = jsonObject.getString("jKNiaosuanCode");
            String userCode = jsonObject.getString("userCode");
            String niaoSuan = jsonObject.getString("niaoSuan");
            int fromType = jsonObject.getInteger("fromType");
            String inspectTimeStr = jsonObject.getString("InspectTime");
            Date inspectTime=TimeStrToDate(inspectTimeStr);

            JKNiaosuan jkNiaosuan = jkNiaosuanService.getByJkNiaosuanCode(jkNiaosuanCode);
            jkNiaosuan.setUserCode(userCode);
            jkNiaosuan.setNiaoSuan(niaoSuan);
            jkNiaosuan.setFromType(fromType);
            jkNiaosuan.setInspectTime(inspectTime);
            jkNiaosuanService.update(jkNiaosuan);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKNiaosuan/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //删除用户尿酸检查记录
    @RequestMapping(value = "Delete",method = RequestMethod.POST)
    public MyResult Delete(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKNiaosuan/Delete");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String jkNiaosuanCode = jsonObject.getString("jKNiaosuanCode");

            jkNiaosuanService.delete(jkNiaosuanCode);

            Object data = new Object[] { };
            return success(data);
        }catch (Exception e){
            insertError("/JKNiaosuan/Delete", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //用户尿酸检查记录列表(app)
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/JKNiaosuan/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            String userCode = jsonObject.getString("userCode");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime = DateUtil.timeAddDay(endTime, 1);
            PageInfo<JKNiaosuan> niaosuanPageInfo = jkNiaosuanService.getPageList(pageIndex, pageSize, userCode,startTime,endTime);

            return success(niaosuanPageInfo);
        }catch (Exception e){
            insertError("/JKNiaosuan/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //用户尿酸检查记录列表
    @RequestMapping(value = "PageAdm",method = RequestMethod.POST)
    public MyResult PageAdm(@RequestBody JSONObject jsonObject) {
        try{
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/JKNiaosuan/PageAdm");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }



            String name = jsonObject.getString("name");
            int pageIndex=jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime = DateUtil.timeAddDay(endTime, 1);

            Staff staff = staffService.getByStaffCode(staffCode);

            PageInfo<JKNiaosuanDTO> niaosuanDTOPageInfo = jkNiaosuanService.getAdmPageList(pageIndex, pageSize, name, staff.getGovernmentCode(), startTime, endTime);

            return success(niaosuanDTOPageInfo);
        }catch (Exception e){
            insertError("/JKNiaosuan/PageAdm", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    
}
